package com.zekdot.fileconsumer.action;

import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * Create by zekdot on 20-4-19.
 */
@Controller
@RequestMapping("/file")
public class FileAction {
    @Autowired
    private RestTemplate restTemplate;
    /**
     * 上传其他文件
     *
     */
    @RequestMapping(value = "/uploadFile",method = RequestMethod.POST)
    @ResponseBody
    public String uploadFile(String userId,@RequestParam(value = "file") MultipartFile file,
                             @RequestParam(value = "maxSize",defaultValue = "32")int maxSize) throws IOException {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);
        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();

        //生成临时文件
        File localFile = new File("/home/zekdot/register/tmps","/"+file.getOriginalFilename());
        file.transferTo(localFile);
        FileSystemResource resource = new FileSystemResource(localFile);

        parammap.add("userId",userId);
        parammap.add("file",resource);
        parammap.add("maxSize",maxSize);
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://file-client/file/uploadFile",entity,String.class);
    }
    /**
     * 上传图片文件
     */
    @RequestMapping(value = "/uploadPic",method = RequestMethod.POST)
    @ResponseBody
    public String uploadPic(String userId,@RequestParam(value = "pic") MultipartFile pic,
                            @RequestParam(value = "maxSize",defaultValue = "32")int maxSize) throws IOException {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);
        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();

        //生成临时文件
        File localFile = new File("/home/zekdot/register/tmps","/"+pic.getOriginalFilename());
        pic.transferTo(localFile);
        FileSystemResource resource = new FileSystemResource(localFile);


        parammap.add("userId",userId);
        parammap.add("pic",resource);
        parammap.add("maxSize",maxSize);
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://file-client/file/uploadPic",entity,String.class);
    }

    /**
     * 上传base64编码的图片文件
     * @param picCode 编码后的图片
     * @param userId 用户id
     * @param maxSize 最大容量
     * @return
     */
    @RequestMapping(value = "/uploadPicByCode",method = RequestMethod.POST)
    @ResponseBody
    public String uploadPicByCode(String picCode,String userId,int maxSize){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();

//        System.out.println(picCode);
        parammap.add("userId",userId);
        parammap.add("picCode",picCode);
        parammap.add("maxSize",maxSize);
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://file-client/file/uploadPicByCode",entity,String.class);
    }
}
